<?php

class UsersController extends Edm_Controller_Action
{

    public function indexAction ()
    {
        $this->view->title = '用户中心';
        $this->view->headLink()->appendStylesheet('/css/common/pager.css');
        $filter = new Zend_Filter_HtmlEntities();
        $data = array(
            'pageSize' => $this->common->defaultPageSize,
            'pageNum' => $filter->filter($this->_request->getQuery('page', 1))
        );
        if ($this->_request->isGet()) {
            $data['keywords'] = $filter->filter($this->_request->getQuery('keywords'));
        }
        $res = $this->_users->userList($data);
        $this->view->search = array('keywords' => $data['keywords']);
        $this->view->list = $res['list'];
        $this->pager->Pager($res['count'], $data['pageSize']);
        $this->view->pageResult = $this->pager->display(2, 'badoo');
    }

    /* 登录 */
    public function loginAction ()
    {
        /* 用户已登录则跳转到用户详细信息 */
        if ($this->view->authenticated && isset($this->view->identity->user_id)) {
            $this->_redirect('/users/detail/');
        }
        $this->view->title = '用户登录';
        $this->view->headScript()->appendFile('/js/common/jquery.validationEngine-cn.js');
        $this->view->headScript()->appendFile('/js/common/jquery.validationEngine.js');
        $this->view->headScript()->appendFile('/js/front/base.js');
        $this->view->headLink()->appendStylesheet('/css/common/validationEngine.jquery.css');

        $filter = new Zend_Filter_HtmlEntities();
        if ($this->_request->isPost()) {
            $data = array(
                    'user_name' => $filter->filter($this->_request->getPost('user_name')),
                    'password' => $filter->filter($this->_request->getPost('password')),
                    'validator' => $filter->filter($this->_request->getPost('validator'))
            );
            $authCode = new Zend_Session_Namespace('Auth_Code');
            if ($authCode->imagecode  == $data['validator']) {
                if ($this->_users->login($data)) {
                    $this->_redirect('/users/detail');
                } else {
                    $error = '抱歉，您输入的用户名或密码错误！';
                    $this->view->error = $error;
                }
            } else {
                $error = '抱歉，您输入的验证码不正确！';
                $this->view->error = $error;
            }
        }
    }

    /* 用户详细信息 */
    public function detailAction ()
    {
        /* 没有登录则跳转到登录 */
        if (!isset($this->view->identity->user_id)) {
            $this->_redirect('/users/login/');
        }
        $this->view->title = '用户详细信息';
        $this->view->userinfo = $this->_users->userInfo(array('user_id' => $this->view->identity->user_id));
    }

    /* 用户注册 */
    public function registerAction () 
    {
        $authCode = new Zend_Session_Namespace('Auth_Code');
        /* 用户已登录则跳转到用户详细信息 */
        if ($this->view->authenticated && isset($this->view->identity->user_id)) {
            $this->_redirect('/users/detail/');
        }
        $this->view->title = '用户注册';
        $this->view->headScript()->appendFile('/js/common/jquery.validationEngine-cn.js');
        $this->view->headScript()->appendFile('/js/common/jquery.validationEngine.js');
        $this->view->headScript()->appendFile('/js/front/base.js');
        $this->view->headLink()->appendStylesheet('/css/common/validationEngine.jquery.css');
        $filter = new Zend_Filter_HtmlEntities();
        if ($this->_request->isPost()) {
            $data = array(
                    'user_name' => $filter->filter($this->_request->getPost('user_name')),
                    'password' => md5($filter->filter($this->_request->getPost('password'))),
                    'email' => $filter->filter($this->_request->getPost('email')),
                    'reg_time' => time()
            );
            $validator = $filter->filter($this->_request->getPost('validator'));

            if ($validator == $authCode->imagecode) {
                /* 如果用户不存在 */
                if (!$this->_users->userCheck(array('user_name' => $data['user_name']))) {
                    $user_id = $this->_users->Insert($data);
                    $auth = Zend_Auth::getInstance();
                    $user = array ('user_id' => $user_id, 'user_name' => $data['user_name'], 'email' => $data['email'], 'reg_time' => $data['reg_time']);
                    $obj = (object)$user;
                    $auth->getStorage()->write($obj);
                    /* 此处不可偷懒 */
                    $msg = array(
                            'msg' => '您已注册成功！',
                            'link' => array(
                                    'text' => array('个人信息', '网站首页'),
                                    'href' => array('/users/detail', '/index')
                            )
                    );
                    $this->view->msg = $msg;
                    $this->renderScript('message/index.phtml');
                } else {
                    $this->view->error = '抱歉，您所输入用户名已经存在！';
                }
            } else {
                $this->view->error = '抱歉，您所输入验证码不正确！';
            }
        }
    }

    /* 用户信息编辑 */
    public function editAction ()
    {
        /* 没有登录则跳转到登录 */
        if (!isset($this->view->identity->user_id)) {
            $this->_redirect('/users/login/');
        }
        $authCode = new Zend_Session_Namespace('Auth_Code');
        $this->view->title = '编辑信息';
        $this->view->headScript()->appendFile('/js/common/jquery.validationEngine-cn.js');
        $this->view->headScript()->appendFile('/js/common/jquery.validationEngine.js');
        $this->view->headScript()->appendFile('/js/front/base.js');
        $this->view->headLink()->appendStylesheet('/css/common/validationEngine.jquery.css');
        $filter = new Zend_Filter_HtmlEntities();
        $this->view->userinfo = $this->_users->userInfo(array('user_id' => $this->view->identity->user_id));
        if ($this->_request->isPost()) {
            $data = array(
                    'user_id' => $filter->filter($this->_request->getPost('user_id')),
                    'password' => $filter->filter($this->_request->getPost('password_old')),
                    'email' => $filter->filter($this->_request->getPost('email'))
            );
            $validator = $filter->filter($this->_request->getPost('validator'));

            if ($validator == $authCode->imagecode) {
                /* 如果原密码正确 */
                if ($this->_users->userCheck($data)) {
                    $data['password'] = md5($filter->filter($this->_request->getPost('repassword')));
                    $user_id = $data['user_id'];
                    unset($data['user_id']);
                    $this->_users->Update($data, $user_id);
                    $msg = array(
                            'msg' => '信息修改成功！',
                            'link' => array(
                                    'text' => array('个人信息', '网站首页'),
                                    'href' => array('/users/detail', '/index')
                            )
                    );
                    $this->view->msg = $msg;
                    $this->renderScript('message/index.phtml');
                } else {
                    $this->view->error = '抱歉，您所输入的原密码不正确！';
                }
            } else {
                $this->view->error = '抱歉，您所输入验证码不正确！';
            }
        }
    }

    /* 检查用户是否存在 */
    public function validatorAction ()
    {
        /* 禁用视图 */
        $this->_helper->viewRenderer->setNoRender();
        $filter = new Zend_Filter_HtmlEntities();
        if ($this->_request->isPost()) {
            $data = array(
                    'user_name' => $filter->filter($this->_request->getPost('user_name')),
                    'user_id' => $filter->filter($this->_request->getPost('user_id')),
                    'password' => $filter->filter($this->_request->getPost('password'))
            );
            echo $this->_users->userCheck($data);
        }
        exit;
    }

    /* 用户退出 */
    public function logoutAction()
    {
        Zend_Auth::getInstance()->clearIdentity();
        $info = new Zend_Session_Namespace('users');
        $info->unsetAll();
        $this->_redirect('/users/');
    }

}
